//
//  GoodfrienViewController.m
//  结伴
//
//  Created by wsl on 16/8/22.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "GoodfrienViewController.h"

#import "GoodOneTableViewCell.h"
#import "GoodTowTableViewCell.h"

@interface GoodfrienViewController ()<UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView_main;

@property (nonatomic,assign) NSInteger num;

@end

@implementation GoodfrienViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    
    
    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"好友",@"群组",nil];
    //初始化UISegmentedControl
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.frame = CGRectMake(28, 23, 88, 33);
    //

    // 设置默认选择项索引
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor = [UIColor whiteColor];


    [segmentedControl addTarget:self action:@selector(selected:) forControlEvents:UIControlEventValueChanged];

    self.navigationItem.titleView = segmentedControl;
    
    [self tableViewHeader];
    
    
}

- (void)tableViewHeader{
    
    
    
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 45)];
    
    
    if (_num == 1) {
        search.placeholder = @"搜索群组";
    }else{
        
        search.placeholder = @"搜索联系人";
        
    }
    
    
    search.delegate = self;

    
    self.tableView_main.tableHeaderView = search;
    
    
}

-(void)selected:(id)sender{
    
    UISegmentedControl* control = (UISegmentedControl*)sender;
    
    
    
    // _num = (int)control.selectedSegmentIndex;
    
    _num = control.selectedSegmentIndex;
    
    
    
    
    [self tableViewHeader];
    
    
     [self.tableView_main reloadData];
    
    
    
}

//返回多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return 5;
}


//cell高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

//返回组头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.1 ;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 0.1;
    
}
//返回cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    id cell;
    
    static NSString *IDOne = @"GoodOneTableViewCell";
    static NSString *IDTow = @"GoodTowTableViewCell";
    //一
    GoodOneTableViewCell *cellone = [tableView dequeueReusableCellWithIdentifier:IDOne];
    
    if (cellone == nil) {
        cellone = [[[NSBundle mainBundle] loadNibNamed:IDOne owner:nil options:nil] lastObject];
    }
    //二
    GoodTowTableViewCell *cellTow = [tableView dequeueReusableCellWithIdentifier:IDTow];
    
    if (cellTow == nil) {
        cellTow = [[[NSBundle mainBundle] loadNibNamed:IDTow owner:nil options:nil] lastObject];
    }

    
    
    if (_num == 0) {
        cell = cellone;
    }else{
        cell = cellTow;
    }
    
    
    
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
